package org.example.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.example.dto.User;
import org.springframework.kafka.core.KafkaOperations;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class TestController {

    @Resource
    private KafkaTemplate<String, String> kafkaTemplate;

    @PostMapping("/send")
    public String send(@RequestBody User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
            kafkaTemplate.send("test",user.getId()+"",objectMapper.writeValueAsString(user));
            return "success";
    }
    @PostMapping("/send2")
    public String send2(@RequestBody User user) throws JsonProcessingException {
        ObjectMapper objectMapper = new ObjectMapper();
        kafkaTemplate.send("test",user.getId()+"",objectMapper.writeValueAsString(user))
                .addCallback((t)->{
                    System.out.println("success"+t.getProducerRecord().value());
                },(e)->{
                    System.out.println(e.getMessage());
                });
        return "success";
    }
}
